/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: mybatis2021 - Demo
 * <p>Powered by webrx On 2021-10-21 20:44:29
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Demo {
    @Test
    public void t1() throws IOException {
        //加载mybatis - core配置文件
        var is = Resources.getResourceAsStream("mybatis-config.xml");
        //根据配置文件，建立核心工厂类
        var sf = new SqlSessionFactoryBuilder().build(is);
        //取得会话
        var ss = sf.openSession();

        System.out.println(ss.selectList("ddbs"));
        System.out.println(ss.selectList("dtbs"));

        //jdbc 接口Connection
        //Connection conn1 = ss.getConnection()
        //Connection conn2 = ss.getConnection()

        //查询数据的版本号
        var ver = ss.selectOne("ver", String.class);
        System.out.println(ver);

        System.out.println("------------------------------");
        var dbs = ss.selectList("dbs");
        dbs.forEach(System.out::println);

        //数据插入
        var map = new HashMap<String, Object>();
        map.put("name", "《计算机编程》");
        map.put("price", 82);
        map.put("author", "James Gosling");
        //int row = ss.insert("add",map);
        //System.out.println(row);

        ss.commit();


        //数据查询
        String bookname = ss.selectOne("queryByIdName", 2);
        System.out.println(bookname);

        Map<String, Object> mm = ss.selectOne("queryById", 2);
        System.out.println(mm);

        var list = ss.selectList("queryAll");
        System.out.println(list);

        //修改id = 4那本书籍的名称 及格式
        Map<String, Object> bookmap = ss.selectOne("queryById", 4);
        bookmap.put("name", "《Java程序设计》");
        bookmap.put("price", "88");
        int nn = ss.update("edit", bookmap);
        System.out.println(nn);
        ss.commit();

        //删除数据
        int yy = ss.delete("delById", 5);
        ss.commit();


    }
}
